﻿using Aspose.Words;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace LeHouse.Server.WebApi.Common
{
    public class CommonWords
    {
        public static string Word(string TemplateName, string SaveName, List<WordsTemp> dic)
        {
            Document doc = new Document(TemplateName);
            foreach (var key in dic)
            {
                if(string.IsNullOrEmpty( key.Value))
                {
                    key.Value = "--";
                }
                string repStr = string.Format("&{0}&", key.Name);
                if (key.Type == FileFormat.Image)
                {
                    //DocumentBuilder builder = new DocumentBuilder(doc);
                    ////将光标移动到指定节点
                    //builder.MoveToBookmark(repStr);
                    ////插入图片
                    //builder.InsertImage(dic[key]);

                    //foreach (var x in key.AllValue)
                    //{
                    //    Regex reg = new Regex(repStr);
                    //    var img = new ReplaceAndInsertImage(x);
                    //    doc.Range.Replace(reg, img, false);
                    //}

                    if (!string.IsNullOrEmpty(key.Value))
                    {
                        if (key.Name == "房屋所有权证照片" || key.Name == "不动产权证书照片" || key.Name == "税收完税证明照片")
                        {
                            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(key.Value)))
                             {
                                 doc.Range.Replace(repStr,"图片地址错误", false, false);
                             }
                            else
                            {
                                Regex reg = new Regex(repStr);
                                var img = new ReplaceAndInsertImage2(System.Web.HttpContext.Current.Server.MapPath(key.Value));
                                doc.Range.Replace(reg, img, false);
                            }
                        }
                        else
                        {
                            if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(key.Value))== false)
                            {
                                doc.Range.Replace(repStr, "图片地址错误", false, false);
                            }
                            else
                            {
                                Regex reg = new Regex(repStr);
                                var img = new ReplaceAndInsertImage(System.Web.HttpContext.Current.Server.MapPath(key.Value));
                                doc.Range.Replace(reg, img, false);
                            }
                        }
                    }
                }
                else
                {
                    //foreach (var x in key.AllValue)
                    //{
                    //    doc.Range.Replace(repStr, x, false, false);
                    //}

                    doc.Range.Replace(repStr, key.Value, false, false);
                }
            }
            doc.Save(SaveName);//也可以保存为1.doc 兼容03-07
            return SaveName;
        }
    }
}